package net.xt.testthread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

public class LockSupportDemo {

    public static void main(String[] args) {
        /**
         * 执行顺序：
         * t1	 -----come in
         * t2	 -----发出通知
         * t1	 -----被唤醒
         */

        // 线程t1启动
        Thread t1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "--------come in");
            LockSupport.park(); // 阻塞当前线程

            System.out.println(Thread.currentThread().getName() + "--------被唤醒");
        }, "t1");
        t1.start();

        try {
            TimeUnit.SECONDS.sleep(1);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 线程t2启动
        Thread t2 = new Thread(() -> {
            LockSupport.unpark(t1);
            System.out.println(Thread.currentThread().getName() + "--------发出通知");
        }, "t2");
        t2.start();
    }

}
